﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnpic.DBUtility;//Please add references
namespace jnpic.DAL
{
    /// <summary>
    /// 数据访问类:SysLog
    /// </summary>
    public partial class SysLog
    {
        public SysLog()
        { }
        #region  Method
     

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(jnpic.Model.SysLog model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@LogID", SqlDbType.Int,4),
					new SqlParameter("@IP", SqlDbType.NVarChar,20),
					new SqlParameter("@Page", SqlDbType.NVarChar,200),
					new SqlParameter("@Message", SqlDbType.Text),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@AddUserID", SqlDbType.Int,4)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.IP;
            parameters[2].Value = model.Page;
            parameters[3].Value = model.Message;
            parameters[4].Value = model.AddTime;
            parameters[5].Value = model.AddUserID;

            DbHelperSQL.RunProcedure("SysLog_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int LogID)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@LogID", SqlDbType.Int,4)
};
            parameters[0].Value = LogID;

            DbHelperSQL.RunProcedure("SysLog_Delete", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string LogIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysLog ");
            strSql.Append(" where LogID in (" + LogIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public jnpic.Model.SysLog GetModel(int LogID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@LogID", SqlDbType.Int,4)
};
            parameters[0].Value = LogID;

            jnpic.Model.SysLog model = new jnpic.Model.SysLog();
            DataSet ds = DbHelperSQL.RunProcedure("SysLog_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["LogID"].ToString() != "")
                {
                    model.LogID = int.Parse(ds.Tables[0].Rows[0]["LogID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IP"] != null)
                {
                    model.IP = ds.Tables[0].Rows[0]["IP"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Page"] != null)
                {
                    model.Page = ds.Tables[0].Rows[0]["Page"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Message"] != null)
                {
                    model.Message = ds.Tables[0].Rows[0]["Message"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddUserID"].ToString() != "")
                {
                    model.AddUserID = int.Parse(ds.Tables[0].Rows[0]["AddUserID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LogID,IP,Page,Message,AddTime,AddUserID ");
            strSql.Append(" FROM SysLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" LogID,IP,Page,Message,AddTime,AddUserID ");
            strSql.Append(" FROM SysLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

      

        #endregion  Method
    }
}

